<?php

namespace App\Http\Controllers\Common;

use App\Http\Controllers\BaseController;
use App\Http\Controllers\Order\SaleorderController;
use App\Models\Common\EditLog;
use Illuminate\Http\Request;


class EditLogController extends BaseController
{

    public function getlist(Request $request)
    {
        $isgetall = $request->isgetall ?? 0;
        $list = EditLog::getList($request, $this->mangerinfo, $isgetall);
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $list]);
    }

    public function getinfo(Request $request)
    {
        $id = $request->id;
        if (!$id) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        $info = EditLog::where(['id'=>$id,'cpid'=>$this->mangerinfo['cpid']])->first();

        if (!$info) {
            return response()->json(['code' => '002', 'msg' => '数据不存在']);
        }else{
            $info = $info->toArray();
        }


        $info['before_data']=json_decode($info['before_data'],true);
        $info['after_data']=json_decode($info['after_data'],true);
        if(isset($info['before_data']) && isset($info['before_data']['suborder'])){
            foreach ($info['before_data']['suborder'] as $key=>$item){
                if(isset($item['accrual'])){
                    $info['before_data']['suborder'][$key]['accrual'] = SaleorderController::amountTransformIntoYuan($item['accrual']);
                }
                if(isset($item['performance'])){
                    $info['before_data']['suborder'][$key]['performance'] = SaleorderController::amountTransformIntoYuan($item['performance']);
                }
                if(isset($item['total'])){
                    $info['before_data']['suborder'][$key]['total'] = SaleorderController::amountTransformIntoYuan($item['total']);
                }
//                if(isset($item['total_weight'])){
//                    $info['before_data']['suborder'][$key]['total_weight'] = SaleorderController::weightTransformIntoTon($item['total_weight']);
//                }
            }
        }
        if(isset($info['after_data']) && isset($info['after_data']['suborder'])){
            foreach ($info['after_data']['suborder'] as $key=>$item){
                if(isset($item['accrual'])){
                    $info['after_data']['suborder'][$key]['accrual'] = SaleorderController::amountTransformIntoYuan($item['accrual']);
                }
                if(isset($item['performance'])){
                    $info['after_data']['suborder'][$key]['performance'] = SaleorderController::amountTransformIntoYuan($item['performance']);
                }
                if(isset($item['total'])){
                    $info['after_data']['suborder'][$key]['total'] = SaleorderController::amountTransformIntoYuan($item['total']);
                }
//                if(isset($item['total_weight'])){
//                    $info['after_data']['suborder'][$key]['total_weight'] = SaleorderController::weightTransformIntoTon($item['total_weight']);
//                }
            }
        }

        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => ['info' => $info]]);
    }
}
